﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Himp.Platform.Api
{
    public static class OpenApiControllerHelper
    {
        /// <summary>
        /// 标头验证
        /// </summary>
        /// <param name="infNo"></param>
        /// <param name="msgId"></param>
        /// <param name="input"></param>
        /// <param name="sk"></param>
        /// <param name="cainfo"></param>
        /// <returns></returns>
        public static bool ValidateHeader(string infNo, string msgId, string input, string sk, string cainfo)
        {
            string inputValue = $"{infNo}{msgId}{input}{sk}";
            var md5 = OpenApiControllerHelper.Md5(inputValue);

            return cainfo == md5;
        }

        /// <summary>
        /// 128位处理key 
        /// </summary>
        /// <param name="keyArray">原字节</param>
        /// <param name="key">处理key</param>
        /// <returns></returns>
        private static byte[] GetAesKey(byte[] keyArray, string key)
        {
            byte[] newArray = new byte[16];

            if (keyArray.Length < 16)
            {
                for (int i = 0; i < newArray.Length; i++)
                {
                    if (i >= keyArray.Length)
                    {
                        newArray[i] = 0;
                    }
                    else
                    {
                        newArray[i] = keyArray[i];
                    }
                }

                return newArray;
            }
            else
            {
                return keyArray;
            }
        }

        /// <summary>
        /// 使用AES加密字符串,按128位处理key
        /// </summary>
        /// <param name="content">加密内容</param>
        /// <param name="key">秘钥，需要128位、256位.....</param>
        /// <returns>Base64字符串结果</returns>
        public static string AesEncrypt(string content, string key, bool autoHandle = true)
        {
            byte[] keyArray = Convert.FromBase64String(key);

            if (autoHandle)
            {
                keyArray = GetAesKey(keyArray, key);
            }

            byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);
            SymmetricAlgorithm des = Aes.Create();
            des.Key = keyArray;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = des.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray);
        }

        /// <summary>
        /// 使用AES解密字符串,按128位处理key
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="key">秘钥，需要128位、256位.....</param>
        /// <returns>UTF8解密结果</returns>
        public static string AesDecrypt(string content, string key, bool autoHandle = true)
        {
            byte[] keyArray = Convert.FromBase64String(key);

            if (autoHandle)
            {
                keyArray = GetAesKey(keyArray, key);
            }

            byte[] toEncryptArray = Convert.FromBase64String(content);
            SymmetricAlgorithm des = Aes.Create();
            des.Key = keyArray;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = des.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        public static string Md5(string inputValue)
        {
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "");
            }
        }
    }
}
